class ListsController < ApplicationController

  before_filter :protect
  def show
    @list = List.find(params[:id])
    @friendlist= Friendlist.find_all_by_list_id(@list.id,:select=> "*", :joins=>"INNER JOIN specs ON specs.user_id=friendlists.user_id INNER JOIN users ON specs.user_id=users.id")
  end

  def destroy
    @list = List.find params[:id]
    @middleware=RestClient.delete('http://127.0.0.1/midsocial/g/list-friends', :params=>{:fb_access_token=>session[:facebook_token],
          :tt_access_token=>session[:twitter_token], :tt_access_token_secret=>session[:twitter_secret],
          :facebook_id=>@list.facebook_id, :twitter_id=>@list.twitter_id})
    if @list.destroy
      logger.debug '===========MIDLEWARE LIST DESTROY==========='
      logger.debug @middleware
      logger.debug '============================================'
      flash[:notice] = "La lista se ha eliminado."
    end
    redirect_to :action=>"index"
  end

  def edit
    @list = List.find params[:id]
    @members = Friendlist.find_by_sql "SELECT users.id, CONCAT(specs.first_name,' ',specs.last_name)
    as nombre, users.facebook_id, users.twitter_id, users.linkedin_id FROM friendlists INNER JOIN users ON
    friendlists.user_id=users.id INNER JOIN specs ON specs.user_id=friendlists.user_id WHERE list_id="+params[:id]
    @friends = Friendship.find_by_sql "SELECT users.id,
    CONCAT(specs.first_name,' ',specs.last_name) as nombre, users.facebook_id, users.twitter_id, users.linkedin_id
    FROM friendships INNER JOIN users ON friendships.friend_id=users.id INNER JOIN specs ON specs.user_id=friendships.friend_id
    WHERE friendships.status='accepted' AND friendships.user_id="+(session[:user_id]).to_s+ " AND friendships.friend_id NOT IN
    (SELECT user_id FROM friendlists WHERE list_id="+params[:id]+")"

  end

  def index
    @title= "Sus listas"
    @lists = List.find :all
  end

  def new
    @title="Cree listas de amigos"
    if param_posted?(:lists)
      @list = List.new(params[:lists])
      @list.user_id=session[:user_id]
      @middleware=RestClient.post('http://127.0.0.1/midsocial/g/list-friends',
        {:fb_access_token=>session[:facebook_token],
          :tt_access_token=>session[:twitter_token], :tt_access_token_secret=>session[:twitter_secret],
          :name=>@list.name, :description=>@list.description, :mode=>"public", :type=>"close_friends"} )
      logger.debug '===========MIDLEWARE LIST CREATE==========='
      logger.debug @middleware
      logger.debug '============================================'
      @parsed_json = JSON.parse(@middleware)
      @list.facebook_id=@parsed_json['facebook']['id']
      @list.twitter_id=@parsed_json['twitter']['id']
      if @list.save
        flash[:notice] = "Lista #{@list.name} creada!"
        redirect_to :action => "index"
      end

    end
  end

  def remove_member
    @member=Friendlist.find_by_user_id_and_list_id(params[:member],params[:id])
    @user=User.find params[:member]
    @list=List.find params[:id]
    @member.destroy
    @middleware=RestClient.delete('http://127.0.0.1/midsocial/g/member-list',
       :params=>{:fb_access_token=>session[:facebook_token],
          :tt_access_token=>session[:twitter_token], :tt_access_token_secret=>session[:twitter_secret],
          :fb_user_id=>@user.facebook_id, :fb_list_id=>@list.facebook_id, :tt_user_id=>@user.twitter_id,
          :tt_list_id=>@list.twitter_id} )
    logger.debug '===========MIDDLEWARE MEMEBER DESTROY========='
    logger.debug @middleware
    logger.debug '============================================'
    redirect_to edit_list_path
  end

  def add_member
    @friend=Friendlist.new(:list_id=>params[:id],:user_id=>params[:friend])
    @list=List.find params[:id]
    @user=User.find params[:friend]
    if @friend.save
      @middleware=RestClient.post('http://127.0.0.1/midsocial/g/member-list',
        {:fb_access_token=>session[:facebook_token],
          :tt_access_token=>session[:twitter_token], :tt_access_token_secret=>session[:twitter_secret],
          :fb_user_id=>@user.facebook_id, :fb_list_id=>@list.facebook_id, :tt_user_id=>@user.twitter_id,
          :tt_list_id=>@list.twitter_id} )
      logger.debug '===========MIDLEWARE MEMEBER CREATE========='
      logger.debug @middleware
      logger.debug '============================================'
    end
    redirect_to edit_list_path
  end

  # Protect a page from unauthorized access.
  def protect
    unless logged_in?
      session[:protected_page] = request.fullpath
      flash[:notice] = "Favor de firmarse primero."
      redirect_to :controller=>"user", :action => "login"
    return false
    end
  end
end
